package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.github.pagehelper.PageInfo;
import com.woniuxy.annotation.MyLog;
import com.woniuxy.entity.*;
import com.woniuxy.entity.excel.HospitalDTO;
import com.woniuxy.service.emp.EmpService;
import com.woniuxy.service.hospital.HospitalService;
import com.woniuxy.service.location.CityService;
import com.woniuxy.service.location.CountyService;
import com.woniuxy.service.location.ProvinceService;
import com.woniuxy.util.ResultVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class HospitalController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private EmpService empService;

    @PostMapping("hospital/findAll")
    public ResultVo findAll(@RequestBody Map<String, String> map) {

        String strPage = map.remove("page");
        String strSize = map.remove("size");
        PageInfo<Hospital> hospitals = hospitalService.findAll(Integer.valueOf(strPage), Integer.valueOf(strSize));
        return ResultVo.success(hospitals);
    }

    /**
     * 导入医院Excel数据
     *
     * @param file 导入的数据文件
     * @return
     * @throws IOException
     */
    @ApiOperation("Excel数据的批量导入")
    @PostMapping("hospital/import")
    public ResultVo importHospitalDataFromExcel(
            @ApiParam(value = "医院 Excel 数据文件", required = true)
            @RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            hospitalService.importHospitalDataFromExcel(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("Excel数据导入成功!");
    }

    /**
     * Excel数据报表下载
     *
     * @param response
     * @throws IOException
     */
    @ApiOperation("Excel数据报表下载")
    @GetMapping("hospital/download")
    public void exportHospitalDataFromExcel(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("医院数据报表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), HospitalDTO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("医院数据报表")
                .doWrite(hospitalService.hospitalData());
    }

    @MyLog("修改医院信息")
    @PostMapping("hospital/update")
    public ResultVo update(@RequestBody Hospital hospital) {
        int rows = hospitalService.update(hospital);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    @MyLog("添加医院")
    @PostMapping("hospital/save")
    public ResultVo save(@RequestBody Hospital hospital) {
        int rows = hospitalService.save(hospital);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }


    @MyLog("删除医院")
    @PostMapping("hospital/delete")
    public ResultVo delete(@RequestBody String hId) {
//        if (!StringUtils.hasText(hId)) {
//            throw new RuntimeException("hId is required");
//        }
        int rows = hospitalService.delete(Integer.valueOf(hId));
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }


    @PostMapping("hospital/findAllProvince")
    public ResultVo findAllProvince() {
        List<Province> province = provinceService.findAllProvince();
        return ResultVo.success(province);
    }

    @PostMapping("hospital/findAllCity")
    public ResultVo findAllCity(@RequestBody String provinceId) {
        List<City> all = cityService.findAllCity(Integer.valueOf(provinceId));
        return ResultVo.success(all);
    }

    @PostMapping("hospital/findAllCounty")
    public ResultVo findAllCounty(@RequestBody String cityId) {
        List<County> all = countyService.findAllCounty(Integer.valueOf(cityId));
        return ResultVo.success(all);
    }

    @PostMapping("hospital/findLike")
    public ResultVo findLike(@RequestBody String str) {

        System.out.println("模糊查询执行啦*******************************************");
        if (str == null) {
            throw new RuntimeException("str为空");
        }
        List<Hospital> list = hospitalService.findLike(str);
        return list != null ? ResultVo.success(list) : ResultVo.failure();
    }

    /**
     * 查询所有医院管理员
     * @return
     */
    @PostMapping("hospital/findAllDockUser")
    public ResultVo findAllDockUser(){
        List<Emp> list = hospitalService.findAllDockUser();
        return list != null ? ResultVo.success(list) : ResultVo.failure();
    }

    @PostMapping("hospital/findDockUserByHId")
    public ResultVo findDockUserByHId(@RequestBody String hId){
        System.out.println(hId);
        List<Emp> emp = hospitalService.findDockUserByHId(Integer.valueOf(hId));
        return ResultVo.success(emp);
    }

    /**
     * 查询所有管理员
     * @return
     */
    @PostMapping("hospital/findAllEmp")
    public ResultVo findAllEmp(){
        List<Emp> list = empService.findAll();
        return ResultVo.success(list);
    }
}
